#include <numeric>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cctype>
#include <cmath>
using namespace std;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
#define Rep(i,n) for(int i=0;i<(n);++i) 
#define For(i,a,b) for(int i=(a);i<=(b);++i) 
#define ForI(TP,i,coll) for(TP (i)=coll.begin();(i)!=coll.end();(i)++)
#define ForD(i,a,b) for(int i=(a);i>=(b);--i)
#define All(a) a.begin(), a.end()

inline double sqr(double d){return d*d;}
inline double dist(double x1, double y1, double x2, double y2)
{
	return sqrt(sqr(x1-x2)+sqr(y1-y2));
}


struct POI{
	int id;
	int level;
	double x, y;
};
bool comp_level(const POI& p1, const POI& p2)
{ return p1.level<p2.level;}

bool comp_x(const POI* p1, const POI* p2)
{ return p1->x<p2->x;}

double dist(POI* p1, POI* p2)
{ return dist(p1->x, p1->y, p2->x, p2->y); }

typedef vector<POI> VPOI;
typedef vector<POI*> VPPOI;
typedef map<string, VPOI> MSP;
MSP pois;

int main()
{
	int N, M;
	cin>>N;
	Rep(i, N)
	{
		string type;
		POI poi;
		cin>>poi.id>>type;
		cin>> poi.level >> poi.x >> poi.y;
		pois[type].push_back(poi);
	}

	// construct
	ForI(MSP::iterator, it, pois)
	{
		sort(All(it->second), comp_level);
	}

	// work

	cin>>M;
	Rep(i, M)
	{
		string ty[3];
		POI l1, l2;
		Rep(j, 3)
			cin>> ty[j];
		cin>> l1.level >> l2.level;

		VPOI& v1=pois[ty[0]];
		VPOI& v2=pois[ty[1]];
		VPOI& v3=pois[ty[2]];

		// go
		VPOI::iterator it11=lower_bound(All(v1), l1, comp_level);
		VPOI::iterator it12=upper_bound(All(v1), l2, comp_level);
		VPOI::iterator it21=lower_bound(All(v2), l1, comp_level);
		VPOI::iterator it22=upper_bound(All(v2), l2, comp_level);
		VPOI::iterator it31=lower_bound(All(v3), l1, comp_level);
		VPOI::iterator it32=upper_bound(All(v3), l2, comp_level);

		VPPOI vp1, vp2, vp3;

		// search
		for(VPOI::iterator it1=it11; it1!=it12; it1++)
			vp1.push_back(&(*it1));
		for(VPOI::iterator it2=it21; it2!=it22; it2++)
			vp2.push_back(&(*it2));
		for(VPOI::iterator it3=it31; it3!=it32; it3++)
			vp3.push_back(&(*it3));

		sort(All(vp1), comp_x);
		sort(All(vp2), comp_x);
		sort(All(vp3), comp_x);

		double md=1e15;
		int sel[3];
		Rep(i, vp1.size())
		{
			POI* p1=vp1[i];
			Rep(j, vp2.size())
			{
				POI* p2=vp2[j];

				if(p2->x + md < p1->x)
					continue;
				if(p2->x - md > p1->x)
					break;
				Rep(k, vp3.size())
				{
					POI* p3=vp3[k];

					if(p3->x + md < p1->x)
						continue;
					if(p3->x - md > p1->x)
						break;

					if(p3->x + md < p2->x)
						continue;
					if(p3->x - md > p2->x)
						break;

					double nd(dist(p1, p2)+dist(p2, p3)+dist(p1, p3));
					if(nd<md)
					{
						md=nd;
						sel[0]=p1->id;
						sel[1]=p2->id;
						sel[2]=p3->id;
					}
				}
			}
		}

	}

	return 0;
}